package cz.muni.fi.xvano1.trinity.beans;

import cz.muni.fi.xvano1.trinity.data.Event;
import cz.muni.fi.xvano1.trinity.supplementary.ActionManagerBeanContext;
import cz.muni.fi.xvano1.trinity.supplementary.DataSupplier;
import java.util.List;
import net.sourceforge.stripes.action.*;

/**
 * This class acts as a base for options of front end. Menu of the front end is
 * implemented with the framework Stripes with this class.
 *
 * @author Jan Vano
 */
public class ActionManagerActionBean implements ActionBean {

    private ActionManagerBeanContext context;
    private String response;

    /**
     * Interface method from ActionBean.
     */
    @Override
    public void setContext(ActionBeanContext context) {
        this.context = (ActionManagerBeanContext) context;
    }

    /**
     * Interface method from ActionBean, using a co-variant return type.
     */
    @Override
    public ActionManagerBeanContext getContext() {
        return this.context;
    }

    /**
     * Redirect user to homepage.
     *
     * @return Resolution objct to framework.
     */
    @DefaultHandler
    public Resolution home() {
        return new RedirectResolution("/Home");
    }

    /**
     * Forwards request to page where users reservations are shown. It loads all
     * necessary information before forwarding.
     *
     * @return Resolution to framework with necessary data
     */
    public Resolution load() {
        String user = getContext().getUser();
        List<Event> eventList = DataSupplier.loadEvents(user);

        if (eventList == null) {
            response = "You do not have any reservations in the system.";
            getContext().getRequest().setAttribute("action", "notice");
            getContext().getRequest().setAttribute("notice", response);
            return new ForwardResolution("/Index");
        } else {

            getContext().getRequest().setAttribute("action", "load");
            getContext().getRequest().setAttribute("eventList", eventList);

           return new ForwardResolution("/Index");
        }
    }

    /**
     * Forwards request to page where user can make new reservations. It sets
     * appropriate attributes to request and passes it to index page.
     *
     * @return Resolution to framework with necessary data
     */
    public Resolution add() {
        getContext().getRequest().setAttribute("action", "add");
        return new ForwardResolution("/Index");
    }

    /**
     * Forwards request to page where user can edit actual reservations. It sets
     * appropriate attributes to request and passes it to index page.
     *
     * @return Resolution to framework with necessary data
     */
    public Resolution edit() {
        getContext().getRequest().setAttribute("action", "edit");
        return new ForwardResolution("/Index");
    }

    /**
     * Forwards request to page where user can delete his/her reservations. It
     * sets appropriate attributes to request and passes it to index page.
     *
     * @return Resolution to framework with necessary data
     */
    public Resolution remove() {
        getContext().getRequest().setAttribute("action", "remove");
        return new ForwardResolution("/Index");
    }
}
